package com.llc.gulimall.coupon.service.impl;

import com.llc.gulimall.coupon.entity.SeckillSkuRelationEntity;
import com.llc.gulimall.coupon.service.SeckillSkuRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.llc.common.utils.PageUtils;
import com.llc.common.utils.Query;

import com.llc.gulimall.coupon.dao.SeckillSessionDao;
import com.llc.gulimall.coupon.entity.SeckillSessionEntity;
import com.llc.gulimall.coupon.service.SeckillSessionService;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;


@Service("seckillSessionService")
public class SeckillSessionServiceImpl extends ServiceImpl<SeckillSessionDao, SeckillSessionEntity> implements SeckillSessionService {

    @Autowired
    SeckillSkuRelationService seckillSkuRelationService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SeckillSessionEntity> page = this.page(
                new Query<SeckillSessionEntity>().getPage(params),
                new QueryWrapper<SeckillSessionEntity>()
        );

        return new PageUtils(page);
    }
    /**
     *
     * @author llc
     * @description 查询最近三天的秒杀活动
     * @date 2022/2/19 14:29
     * @param
     * @return java.util.List<com.llc.gulimall.coupon.entity.SeckillSessionEntity>
     */
    @Override
    public List<SeckillSessionEntity> getLatest3DaySession() {
        //计算最近三天的时间
        List<SeckillSessionEntity> list = this.list(new QueryWrapper<SeckillSessionEntity>().between("start_time", startTime(), endTime()));
        //计算这商品所需要的商品
        if (!CollectionUtils.isEmpty(list)) {
            // 为每个场次保存活动信息
            List<SeckillSessionEntity> collect = list.stream().map(session -> {
                //活动id
                Long id = session.getId();
                List<SeckillSkuRelationEntity> relationEntities = seckillSkuRelationService.list(new QueryWrapper<SeckillSkuRelationEntity>().eq("promotion_session_id", id));
                //session.setRelationSkus(seckillSkuRelationService.getRelationSkusBySessionId(id));
                session.setRelationSkus(relationEntities);
                return session;
            }).collect(Collectors.toList());
            return collect;
        }
        return null;
    }

    /**
     * 最近三天的开始时间
     * @return
     */
    private String startTime() {
        LocalDate now = LocalDate.now();
        String start = LocalDateTime.of(now, LocalTime.MIN).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        return start;
    }

    /**
     * 最近三天的结束时间
     * @return
     */
    private String endTime() {
        LocalDate after2Days = LocalDate.now().plusDays(2);
        String end = LocalDateTime.of(after2Days, LocalTime.MAX).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        return end;
    }


}